package com.qitianyong.extendUI;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.qitianyong.selfclass.LogUtils;
import com.qitianyong.sgkusbcamera.R;

import static android.content.DialogInterface.BUTTON_NEGATIVE;
import static android.content.DialogInterface.BUTTON_POSITIVE;

/**
 * Created by qitianyong on 2016/11/9.
 */

public class MessageBox{

    private static MessageBox msgBox;

    public interface onEvent{
       void onAlertClick(DialogInterface dialog, int id, int flag, MessageBox msgBox);
    }

    public AlertDialog __dialog;
    private TextView __tv_title;
    private Button __btn_ok, __btn_cancel;
    private ImageView __iv_exit_animate;
    private RelativeLayout __rl_opt_btn;

    private onEvent __onEvent;
    private static int __flag = 0;
    private DialogInterface.OnClickListener __onClickListener = new DialogInterface.OnClickListener(){

        @Override
        public void onClick(DialogInterface dialog, int which) {
            __onEvent.onAlertClick(dialog, which, __flag, MessageBox.this);
        }
    };

    public static MessageBox ShowDefaultMessageBox(Activity context, onEvent event, String msg){
        if(msgBox!=null){
            try {
                if (msgBox.__dialog.isShowing())
                    msgBox.__dialog.cancel();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        msgBox = new MessageBox(context,
                 msg, context.getString(R.string.cancel), context.getString(R.string.ok));

        msgBox.setOnAlertClickListener(event);
        msgBox.doModal();

        __flag = 0;
        return msgBox;
    }
    public static MessageBox ShowConfirmMessageBox(Activity context, onEvent event, String msg){
        if(msgBox!=null){
            try {
                if (msgBox.__dialog.isShowing())
                    msgBox.__dialog.cancel();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        msgBox = new MessageBox(context,
                msg, context.getString(R.string.cancel), context.getString(R.string.ok));

        msgBox.setOnAlertClickListener(event);
        msgBox.__btn_cancel.setVisibility(View.GONE);
        msgBox.__tv_title.setPadding(5,30,5,30);
        msgBox.doModal();

        __flag = 0;
        return msgBox;
    }

    public static void showExDefaultMsgBox(Activity context, onEvent event, String msg, int flag){
        ShowDefaultMessageBox(context, event, msg);
        __flag = flag;
    }

    public MessageBox(Context context, String msg, String ltext, String rtext) {

        __dialog = new AlertDialog.Builder(context).create();
        __dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        View view = View.inflate(context,R.layout.alertdialog_okcancel,null);
        __dialog.setView(view,0,0,0,0);

        __tv_title = (TextView)view.findViewById(R.id.title);
        __btn_ok = (Button)view.findViewById(R.id.btn_positive);
        __btn_cancel = (Button)view.findViewById(R.id.btn_negative);
        __iv_exit_animate = (ImageView)view.findViewById(R.id.iv_exit_animate);
        __rl_opt_btn = (RelativeLayout)view.findViewById(R.id.rl_opt_btn);

        __tv_title.setText(msg);
        __btn_ok.setText(rtext);
        __btn_cancel.setText(ltext);

        __btn_ok.setOnClickListener(mListener);
        __btn_cancel.setOnClickListener(mListener);

        __dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                msgBox = null;
            }
        });
    }

    public void setOnAlertClickListener(onEvent onevent){
        __onEvent = onevent;
    }
    public void setBufferVisible(int visible, String text){
        __iv_exit_animate.setVisibility(visible);
        __tv_title.setText(text);
        __rl_opt_btn.setVisibility(View.GONE);
    }

    public void doModal(){

       if(__dialog == null) return;
        __dialog.show();
    }

    private View.OnClickListener mListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            LogUtils.addClickLog(v.getId());

            switch (v.getId()){
                case R.id.btn_positive:{
                    __onEvent.onAlertClick(__dialog, BUTTON_POSITIVE, __flag, MessageBox.this);

                    break;
                }
                case R.id.btn_negative:{
                    __onEvent.onAlertClick(__dialog, BUTTON_NEGATIVE, __flag, MessageBox.this);
                    break;
                }
                default:break;
            }
        }
    };
}
